<?php require_once('../../core/import.php');?>
<?php User::Permission('PROCUREMENT_UPDATE'); ?>
<?php
    if($_SERVER['REQUEST_METHOD']=='POST')
    {
		
        #图片处理
        $Result=Upload::Probe($_FILES['ThumbnailPath'],Config::UPLOAD_ATTACHMENT_SIZE_IMAGE,Config::UPLOAD_ATTACHMENT_EXT_IMAGE,Config::PATH_UPLOAD_ATTACHMENT_TEMP);
        
        #如果重新上传了图片，处理
        $FileName=NULL;
        if($Result['error']!=UPLOAD_ERR_NO_FILE)    //如果上传了文件
        {
            switch($Result['error'])
            {
                case UPLOAD_ERR_OK:
                    $FileName=Upload::Execute($_FILES['ThumbnailPath'],Config::PATH_UPLOAD_ATTACHMENT_TEMP);
                break;
                case Upload::UPLOAD_ERR_CUSTOM_EXT:
                    exit('{"Number":"2","Description":"缩略图格式不正确，只能是：'.join(',',Upload::$UPLOAD_EXT_IMAGE).'。"}');
                break;
                case Upload::UPLOAD_ERR_CUSTOM_SIZE:
                    exit('{"Number":"3","Description":"缩略图大小超过了限制，不能超过：'.Functions::SizeFormat(Config::UPLOAD_ATTACHMENT_SIZE_IMAGE).'。"}');
                break;
                default :
                    exit('{"Number":"4","Description":"缩略图上传失败。错误代码：'.$Result['error'].'，错误描述：'.$Result['message'].'。"}');
            }
        }
	  
		$validity=Functions::FormArgs('Validity');
        if(!is_numeric($validity)){
             exit('{"Number":"2","Description":"请选择有效期"}');
        }
        $date=date_create();        
        date_add($date,date_interval_create_from_date_string($validity.' days'));
        #上传数据
	  $Data=array
		(
			'ClassID'=>Functions::FormArgs('ClassID'),
			'SubclassID'=>Functions::FormArgs('SubclassID'),
			'Title'=>Functions::FormArgs('Title'),
			'Norms'=>Functions::FormArgs('Norms'),
			'Brand'=>Functions::FormArgs('Brand'),
			'Origin'=>Functions::FormArgs('Origin'),
			'SupplierLocation'=>Functions::FormArgs('SupplierLocation'),
			'TakeAddress'=>Functions::FormArgs('TakeAddress'),
			'Description'=>Functions::FormArgs('Description',NULL,FALSE),
			'Amount'=>Functions::FormArgs('Amount'),
			'Unit'=>Functions::FormArgs('Unit'),
			'Validity'=>date_format($date,'Y-m-d'),
			'IsUrgent'=>Functions::FormArgs('IsUrgent')=='yes' ? 1:0,
			'M_Recommend_Is'=>Functions::FormArgs('M_Recommend_Is')=='yes' ? 1:0,
			'M_ShipyardRecommend_Is'=>Functions::FormArgs('M_ShipyardRecommend_Is')=='yes' ? 1:0,			
			'DateTime_LastUpdated'=>date('Y-m-d G:i:s')
		);
		$Supermarket_Top=DB::GetRows('select * from `procurement` where ID='.Functions::FormArgs('ID').'');
		if(Functions::FormArgs('M_Recommend_Is')=='yes' ? 1:0!=$Supermarket_Top[0]['M_Recommend_Is']) 
		{
			$Data['M_Recommend_Datetime']=date('Y-m-d G:i:s');
		}
		if(Functions::FormArgs('M_ShipyardRecommend_Is')=='yes' ? 1:0!=$Supermarket_Top[0]['M_ShipyardRecommend_Is']) 
		{
			$Data['M_ShipyardRecommend_Datetime']=date('Y-m-d G:i:s');
		}		
		if($FileName)
		{
			$Data['ThumbnailPath']=$FileName ;
		}
		
	  DB::UpdateRowByID('procurement',Functions::FormArgs('ID'),$Data);
		echo '{"Number":"0","Description":"修改成功"}';
    }
?>